using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Boggle.Net.ViewModels
{
    public class Selection<T> : IEnumerable<T> where T : class
    {
        private readonly IList<T> _itemList = new List<T>();

        public event EventHandler Changed = delegate { };

        public bool IsEmpty
        {
            get { return _itemList.Count == 0; }
        }

        public void Push(T item)
        {
            Contract.Requires<ArgumentNullException>(item != null);
            Contract.Requires<ArgumentException>(!this.Contains(item));

            _itemList.Add(item);
            Changed(this, EventArgs.Empty);
        }

        public void Pop()
        {
            Contract.Requires<InvalidOperationException>(!IsEmpty);

            _itemList.Remove(_itemList.Last());
            Changed(this, EventArgs.Empty);
        }

        public void Clear()
        {
            _itemList.Clear();
            Changed(this, EventArgs.Empty);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _itemList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}